package prototype.model;

import prototype.constant.ELanguage;
import prototype.constant.EMessageConst;
import prototype.constant.data.DictionaryConst;
import prototype.data.Dictionary;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.List;

/**
 *
 */
public class DictionaryModel {

	private static final String H_SELECT = "select o from Dictionary o where o." + DictionaryConst.H_LANGUAGE + "=?1 and o." + DictionaryConst.H_CODE + "=?2";

	public static Dictionary select(final EntityManager em, ELanguage language, EMessageConst code) {
		final Query query = em.createQuery(H_SELECT);
		query.setParameter(1, language);
		query.setParameter(2, code);
		Dictionary item = null;
		try {
			item = (Dictionary) query.getSingleResult();
		} catch (NoResultException e) {
		}
		return item;
	}

	private static final String H_LIST = "select o from Dictionary o order by o." + DictionaryConst.H_LANGUAGE + ", o." + DictionaryConst.H_CODE;

	public static List<Dictionary> list(final EntityManager em) {
		final Query query = em.createQuery(H_LIST);
		final List<Dictionary> list = query.getResultList();
		return list;
	}
}
